using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HomePortal.Models;

namespace HomePortal.Controllers
{   
    public class NewsController : Controller
    {
		private readonly INewsRepository newsRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public NewsController() : this(new NewsRepository())
        {
        }

        public NewsController(INewsRepository newsRepository)
        {
			this.newsRepository = newsRepository;
        }

        public ViewResult Index()
        {
            return View(newsRepository.AllIncluding(news => news.Comments));
        }

        [Authorize(Users = "admin")]
        public ViewResult Admin()
        {
            return View(newsRepository.AllIncluding(news => news.Comments));
        }

        [Authorize(Users = "admin")]
        public ViewResult Details(int id)
        {
            return View(newsRepository.Find(id));
        }

        [ValidateInput(false)]
        [Authorize(Users = "admin")]
        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Users = "admin")]
        public ActionResult Create(News news, HttpPostedFileBase image)
        {
            if (ModelState.IsValid) {
                if (image != null)
                {
                    news.ImageMimeType = image.ContentType;
                    news.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(news.ImageData, 0, image.ContentLength);
                }
                newsRepository.InsertOrUpdate(news);
                newsRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        [Authorize(Users = "admin")]
        public ActionResult Edit(int id)
        {
             return View(newsRepository.Find(id));
        }


        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Users = "admin")]
        public ActionResult Edit(News news, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    news.ImageMimeType = image.ContentType;
                    news.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(news.ImageData, 0, image.ContentLength);
                }

                newsRepository.InsertOrUpdate(news);
                newsRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        [HttpGet]
        public FileContentResult GetImage(int newsId)
        {
            var news = newsRepository.Find(newsId);
            if (news!=null)
            {
                return File(news.ImageData, news.ImageMimeType);
            }
            return null;
        }

        [Authorize(Users = "admin")]
        public ActionResult Delete(int id)
        {
            return View(newsRepository.Find(id));
        }

        //
        // POST: /News/Delete/5
        [Authorize(Users = "admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            newsRepository.Delete(id);
            newsRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                newsRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

